package com.cos.shop.service.impl;

import com.cos.shop.common.ResponseResult;
import com.cos.shop.dao.AddressDao;
import com.cos.shop.dao.impl.AddressDaoImpl;
import com.cos.shop.entity.Address;
import com.cos.shop.service.AddressServices;

import java.sql.SQLException;
import java.util.List;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: AddressServicesImpl
 * Author: saber
 * Date:2022/9/19 21:29
 */
public class AddressServicesImpl implements AddressServices {
    private AddressDao ad = new AddressDaoImpl();

    @Override
    public ResponseResult getAddressByUser(int uid) {
        try {
            List<Address> addressList = ad.getUserAddress(uid);
            if (addressList != null && addressList.size() > 0) {
                return ResponseResult.createSuccess(addressList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("暂无地址信息");
    }

    @Override
    public ResponseResult addAddressByUser(Address address) {
        try {
            int i = ad.addUserAddress(address);
            if (i>0){
                return ResponseResult.createSuccess("新增成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("新增失败");
    }

    @Override
    public ResponseResult deleteAddress(int aid) {
        try {
            if (aid>0) {
                if (ad.deleteAddress(aid)>0) {
                    return ResponseResult.createSuccess("删除成功");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("删除失败");
    }


    @Override
    public ResponseResult updateAddress(Address address) {
        try {
            if (ad.updateAddress(address)>0) {
                return ResponseResult.createSuccess("修改成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("修改失败");
    }

    @Override
    public ResponseResult changeDefault(int aid, int uid) {
        try {
            if (aid>0) {
                ad.setAllState(uid);
                ad.setDefaultAddress(aid);
                return ResponseResult.createSuccess("设置默认地址成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("设置默认地址失败");
    }
}
